<?php
namespace addons\mall\controller;

use app\common\model\mall\goods\Cat;

/**
 * 品牌控制器
 */
class Brand extends Base{
	/**
	 * 品牌街
	 */
    public function index()
    {
        $pagesize = $this->request->param('pagesize/d',16);
        $this->assign('pagesize',$pagesize);

        $filterlist = [];
        $orderlist = [];

        $filter = $this->request->get('filter/a',[]);
        $orderby = $this->request->get('orderby', '');
        $orderway = $this->request->get('orderway', '', 'strtolower');
        $params = ['pagesize' => $pagesize];
        //$params = ['pagesize' => $pagesize, 'id' => $cat->id];
        if ($filter)
            $params['filter'] = $filter;
        if ($orderby)
            $params['orderby'] = $orderby;
        if ($orderway)
            $params['orderway'] = $orderway;

        $goodsCats = Cat::where(['status'=>'normal','showis'=>1,'pid'=>0])
            ->order('weigh asc')->column("id,simplename");

        $fields = [
            ['name' => 'cat_id', 'title' => '商品大类', 'content' => $goodsCats],
        ];

        $filter = array_intersect_key($filter, array_flip(array_column($fields, 'name')));
        foreach ($fields as $v) {
            $content = [];
            $all = ['' => __('All')] + $v['content'];
            foreach ($all as $m => $n) {
                $active = ($m === '' && !isset($filter[$v['name']])) || (isset($filter[$v['name']]) && $filter[$v['name']] == $m) ? TRUE : FALSE;
                $prepare = $m === '' ? array_diff_key($filter, [$v['name'] => $m]) : array_merge($filter, [$v['name'] => $m]);
                $url = '?' . http_build_query(array_merge(['filter' => $prepare], array_diff_key($params, ['filter' => ''])));
                $content[] = ['value' => $m, 'title' => $n, 'active' => $active, 'url' => $url];
            }
            $filterlist[] = [
                'name'    => $v['name'],
                'title'   => $v['title'],
                'content' => $content,
            ];
        }


        $sortrank = [
            ['name' => 'weigh', 'title' => __('Default')],
            ['name' => 'name', 'title' => __('Name')],
            ['name' => 'id', 'title' => __('Create time')],
        ];
        $orderby = $orderby && in_array($orderby, ['id', 'visitnum', 'salenum', 'shopprice', 'appraisenum', 'saletime']) ? $orderby : 'id';
        $orderway = $orderway ? $orderway : 'asc';
        foreach ($sortrank as $k => $v) {
            $url = '?' . http_build_query(array_merge($params, ['orderby' => $v['name'], 'orderway' => ($orderway == 'desc' ? 'asc' : 'desc')]));
            $v['active'] = $orderby == $v['name'] ? true : false;
            $v['url'] = $url;
            $orderlist[] = $v;
        }

        $pagelist = \addons\mall\model\Brand::alias('b')
            ->join("mall_goods_catbrand cb","b.id=cb.brand_id","left")
            ->where('b.status', '=','normal')
            ->where($filter)
            ->order($orderby, $orderway)
            ->field('cb.cat_id,b.id,b.name,b.brandimg')
            ->paginate($pagesize, false, ['type' => '\\addons\\mall\\library\\Bootstrap']);

        $pagelist->appends($params);
        $this->view->assign("__FILTERLIST__", $filterlist);
        $this->view->assign("__ORDERLIST__", $orderlist);
        $this->view->assign("__PAGELIST__", $pagelist);
        return $this->fetch("/goods/brand");
    }

}
